<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Prize_entity{
	var $id = 0;
	var $tournament_id = 0;
	var $rank = 0;
	var $prize = "";
	var $image = "";
	var $winner = 0;
	var $courier_name = "";
	var $shipping_date = 0;
	var $tracking_no = "";
	var $description = "";
	var $logo = "";
	var $value = "";
	var $delivery_status = '';
	var $proof = '';
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_PRIZE)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->tournament_id = $result[0]->tournament_id;
			$this->rank = $result[0]->rank;
			$this->prize = $result[0]->prize;
			$this->image = $result[0]->image;
			$this->winner = $result[0]->winner;
			$this->courier_name = $result[0]->courier_name;
			$this->shipping_date = $result[0]->shipping_date;
			$this->tracking_no = $result[0]->tracking_no;
			$this->description = $result[0]->description;
			$this->logo = $result[0]->logo;
			$this->value = $result[0]->value;
			$this->delivery_status = $result[0]->delivery_status;
			$this->proof = $result[0]->proof;
			
			// Create folder if not exists
			$directory = "./uploads/default/prize";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			return $this;
		}
		return false;
	}
	
	function setTournamentId($input){ $this->tournament_id=$input; }
	function setRank($input){ $this->rank=$input;}
	function setPrizeName($input){ $this->prize=$input; }
	function setImage($input){ $this->image=$input;}
	function setWinner($input){ $this->winner=$input; }
	function setCourierName($input){ $this->courier_name=$input;}
	function setShippingDate($input){ $this->shipping_date=$input;}
	function setTrackingNo($input){ $this->tracking_no=$input;}
	function setDescription($input){ $this->description=$input;}
	function setLogo($input){ $this->logo=$input; }
	function setValue($input){ $this->value=$input; }
	function setDeliveryStatus($input){ $this->delivery_status=$input; }
	function setProof($input){ $this->proof=$input; }
	
	function getId(){ return $this->id; }
	function getTournamentId(){ return $this->tournament_id; }
	function getRank(){ return $this->rank;}
	function getPrizeName(){ return $this->prize;}
	function getImage(){ return $this->image;}
	function getWinner(){ return $this->winner;}
	function getCourierName(){ return $this->courier_name;}
	function getShippingDate(){ return $this->shipping_date;}
	function getTrackingNo(){ return $this->tracking_no;}
	function getDescription(){ return $this->description;}
	function getLogo(){ return $this->logo; }
	function getValue(){ return $this->value; }
	function getDeliveryStatus(){ return $this->delivery_status; }
	function getProof(){ return $this->proof; }
	
	function getFolderFileStorage(){ 
		return "./uploads/default/prize/"; 
	}
	
	function getPrizeImage(){
		return $this->getFolderFileStorage().$this->getImage();
	}
	
	function getLogoImage(){
		return $this->getFolderFileStorage().$this->getLogo();
	}
	
	function getProofImage(){
		return $this->getFolderFileStorage().$this->getProof();
	}
	
	function getProofTargetFileUrl(){
		return site_url()."uploads/default/prize/".$this->getProof();
	}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('rank', $this->rank);
		$ci->db->set('prize', $this->prize);
		$ci->db->set('image', $this->image);
		$ci->db->set('winner', $this->winner);
		$ci->db->set('courier_name', $this->courier_name);
		$ci->db->set('shipping_date', $this->shipping_date);
		$ci->db->set('tracking_no', $this->tracking_no);
		$ci->db->set('description', $this->description);
		$ci->db->set('logo', $this->logo);
		$ci->db->set('value', $this->value);
		$ci->db->set('delivery_status', $this->delivery_status);
		$ci->db->set('proof', $this->proof);
		
		$ci->db->where('id', $this->id)->update(TBL_PRIZE);
	}
	
	function createNewPrizeEntity($rank){
		$ci=& get_instance();
		
		$ci->db->set('rank',$rank);
		$ci->db->insert(TBL_PRIZE);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfPrize($tournament_id){
		$ci=& get_instance();
		return $ci->db->where('tournament_id', $tournament_id)->order_by('rank','asc')->get(TBL_PRIZE)->result();
	}
	
}
